/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package j2me.io;

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

/**
 *
 * @author Antonio Diego
 */
public class File {

    // public static final String separator = System.getProperty("file.separator");
    //public static final char separatorChar = separator.charAt(0);
    // public static final String pathSeparator = System.getProperty("path.separator");
    // public static final char pathSeparatorChar = pathSeparator.charAt(0);
    private final String _path;

    private FileConnection conn;

    public File(String path) {
        _path = path.startsWith("file:///") ? path.substring(8, path.length()) : path;
    }

    public String getPath() {
        return _path;
    }

    public String toString() {
        return getPath();
    }

    public String getURL() {
        return getConn().getURL();
    }

    public boolean isDirectory() {
        boolean isDirectory = getConn().isDirectory();

        try {
            conn.close();
            conn = null;
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return isDirectory;
    }

    private FileConnection getConn() {
        if (conn == null) {
            try {
                conn = (FileConnection) Connector.open("file:///" + getPath());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return conn;
    }
}
